﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace JXT.PrimaryKey.Batman.Core.Extensions
{
    /// <summary>
    /// WEB基础类扩展
    /// </summary>
    public static class WebEnvironment
    {
        /// <summary>
        /// 服务器域名
        /// </summary>
        public static string ServerDomain
        {
            get
            {
                string text1 = HttpContext.Current.Request.Url.Host.ToLower().Trim();
                string[] textArray1 = text1.Split(new char[] { '.' });
                if ((textArray1.Length < 3) || System.Text.RegularExpressions.Regex.IsMatch(text1, @"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"))
                {
                    return text1;
                }
                string text2 = text1.Remove(0, text1.IndexOf(".") + 1);
                if ((text2.StartsWith("com.") || text2.StartsWith("net.")) || (text2.StartsWith("org.") || text2.StartsWith("gov.")))
                {
                    return text1;
                }
                return text2;
            }
        }

        /// <summary>
        /// 访问Referrer
        /// </summary>
        public static string Referrer
        {
            get
            {
                Uri uri1 = HttpContext.Current.Request.UrlReferrer;
                if (uri1 == null)
                {
                    return string.Empty;
                }
                return Convert.ToString(uri1);
            }
        }

        /// <summary>
        /// 用户IP
        /// </summary>
        public static string UserIp
        {
            get
            {
                string text1 = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if ((text1 == null) || (text1 == string.Empty))
                {
                    text1 = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (!System.Text.RegularExpressions.Regex.IsMatch(text1, @"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"))
                {
                    return "Unknown";
                }
                return text1;
            }
        }

        /// <summary>
        /// 获取.NET执行级别
        /// </summary>
        /// <returns></returns>
        public static AspNetHostingPermissionLevel GetCurrentTrustLevel()
        {
            foreach (AspNetHostingPermissionLevel trustLevel in
                    new AspNetHostingPermissionLevel[] {
                AspNetHostingPermissionLevel.Unrestricted,
                AspNetHostingPermissionLevel.High,
                AspNetHostingPermissionLevel.Medium,
                AspNetHostingPermissionLevel.Low,
                AspNetHostingPermissionLevel.Minimal 
            })
            {
                try
                {
                    new AspNetHostingPermission(trustLevel).Demand();
                }
                catch (System.Security.SecurityException)
                {
                    continue;
                }

                return trustLevel;
            }

            return AspNetHostingPermissionLevel.None;
        }

        /// <summary>
        /// 增加P3Header
        /// </summary>
        /// <param name="response"></param>
        public static void AddP3PHeader(this HttpResponseBase response)
        {
            response.AddHeader("P3P", "CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA div COM NAV OTC NOI DSP COR\"");
        }
    }
}
